package com.example.wangzh.mytest.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wangzh.mytest.R;
import com.example.wangzh.mytest.databinding.ItemImageScaleTypeBinding;

/**
 * @author: zhwang37
 * @date: 2022/10/13 16:28
 * @email: zhwang37@iflytek.com
 * @describe:
 */
public class ScaleTypeAdapter extends RecyclerView.Adapter<ScaleTypeAdapter.ViewHolder> {
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(
                R.layout.item_image_scale_type, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        viewHolder.bind(i);
    }

    @Override
    public int getItemCount() {
        return 8;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private final ItemImageScaleTypeBinding mBinding;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mBinding = ItemImageScaleTypeBinding.bind(itemView);
        }

        @SuppressLint("SetTextI18n")
        public void bind(int position) {
            switch (position) {
                case 0:
                    mBinding.txtScaleName.setText("matrix");
                    setScaleType(ImageView.ScaleType.MATRIX);
                    break;
                case 1:
                    mBinding.txtScaleName.setText("fit_xy");
                    setScaleType(ImageView.ScaleType.FIT_XY);
                    break;
                case 2:
                    mBinding.txtScaleName.setText("fit_start");
                    setScaleType(ImageView.ScaleType.FIT_START);
                    break;
                case 3:
                    mBinding.txtScaleName.setText("fit_center");
                    setScaleType(ImageView.ScaleType.FIT_CENTER);
                    break;
                case 4:
                    mBinding.txtScaleName.setText("fit_end");
                    setScaleType(ImageView.ScaleType.FIT_END);
                    break;
                case 5:
                    mBinding.txtScaleName.setText("center");
                    setScaleType(ImageView.ScaleType.CENTER);
                    break;
                case 6:
                    mBinding.txtScaleName.setText("center_crop");
                    setScaleType(ImageView.ScaleType.CENTER_CROP);
                    break;
                case 7:
                    mBinding.txtScaleName.setText("center_inside");
                    setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                    break;
                default:
                    break;
            }
        }

        private void setScaleType(ImageView.ScaleType scaleType) {
            mBinding.img1.setScaleType(scaleType);
            mBinding.img2.setScaleType(scaleType);
            mBinding.img3.setScaleType(scaleType);
            mBinding.img4.setScaleType(scaleType);
            mBinding.img5.setScaleType(scaleType);
            mBinding.img6.setScaleType(scaleType);
        }
    }
}
